<?php

/**
 *
 * @author zhuang<pingguoli@gmail.com>
 * @Date 2020/7/7 上午8:28
 *
 * @copyright Copyright 2020 - 2021
 */

namespace App\Http\Controllers\Api\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Assessment;
use App\Models\Child;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Validator;

class ChildController extends BaseController
{
    /**
     * 用户管理学生列表
     *
     * @Date 2020/7/10 下午2:49
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $user = $this->getUser();

        $children = $user->children()->select('id', 'name', 'birthday', 'sex', 'address', 'phone', 'created_at')
            ->simplePaginate(config('site.other.paginate'))->toArray();
        return $this->json($children);
    }

    /**
     * 添加学生记录
     *
     * @Date 2020/7/10 下午2:48
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function add(Request $request)
    {
        $user = $this->getUser();

        $this->validate($request, [
            'name' => 'required|max:255',
            'birthday' => 'required|date',
            'sex' => 'required|in:0,1',
            'address' => 'required|max:255',
            'phone' => 'required|string|max:11',
        ], [], [
            'name' => __("Username"),
            'birthday' => __("Birthday"),
            'sex' => __("Sex"),
            'address' => 'required|max:255',
            'phone' => 'required|string|max:11',
        ]);

        $data = $request->only(['name', 'birthday', 'sex', 'address', 'phone']);
        $data['user_id'] = $user->id;
        $data['agency_id'] = $user->agency_id;

        if (Child::create($data)) {
            return $this->json();
        }

        return $this->json([], config("apicode.serverError.msg"), config("apicode.serverError.code"));

    }

    /**
     * 编辑学生信息
     *
     * @Date 2020/7/10 下午2:51
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function edit(Request $request, $id)
    {
        $child = Child::find($id);

        if (empty($child)) {
            return $this->json([], config('apicode.noRecord.msg'), config('apicode.noRecord.code'));
        }
        //判断用户是否拥有管理学生的权限
        $user = $this->getUser();

        if (!$user->can('allow', $child)) {
            return $this->json([], config('apicode.cannotManage.msg'), config('apicode.cannotManage.code'));
        }

        $this->validate($request, [
            'name' => 'required|max:255',
            'birthday' => 'required|date',
            'sex' => 'required|in:0,1',
            'address' => 'required|max:255',
            'phone' => 'required|string|max:11',
        ], [], [
            'name' => __("Username"),
            'birthday' => __("Birthday"),
            'sex' => __("Sex"),
            'address' => 'required|max:255',
            'phone' => 'required|string|max:11',
        ]);

        $data = $request->only(['name', 'birthday', 'sex', 'address', 'phone']);

        if (!$child->update($data)) {
            return $this->json([], config("apicode.serverError.msg"), config("apicode.serverError.code"));
        }

        return $this->json();
    }

    /**
     *查看学生信息
     *
     * @Date 2020/7/10 下午3:00
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function view($id)
    {

        $child = Child::find($id);

        if (empty($child)) {
            return $this->json([], config('apicode.noRecord.msg'), config('apicode.noRecord.code'));
        }
        //判断用户是否拥有管理学生的权限
        $user = $this->getUser();

        if (!$user->can('allow', $child)) {
            return $this->json([], config('apicode.cannotManage.msg'), config('apicode.cannotManage.code'));
        }

        return $this->json(['child'=>$child->toArray()]);
    }

    /**
     * 删除学生信息
     *
     * @Date 2020/7/10 下午3:00
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id)
    {
        $child = Child::find($id);

        if (empty($child)) {
            return $this->json([], config('apicode.noRecord.msg'), config('apicode.noRecord.code'));
        }

        //判断用户是否拥有管理学生的权限
        $user = $this->getUser();

        if (!$user->can('allow', $child)) {
            return $this->json([], config('apicode.cannotManage.msg'), config('apicode.cannotManage.code'));
        }

        //学生是否有关联数据
        $assessment = Assessment::where("child_id", $id)->get();
        if (count($assessment) > 0) {
            return $this->json([], config('apicode.cannotdelete.msg'), config('apicode.cannotdelete.code'));
        }

        if (!$child->delete()) {
            return $this->json([], config('apicode.serverError.msg'), config('apicode.serverError.code'));
        }
        return $this->json();
    }
}
